/*
 * @Author: lihaogn
 * @Date: 2021-06-02 22:59:28
 * @LastEditTime: 2022-05-26 21:17:44
 * @LastEditor: lihaogn
 * @Description: 登录 service
 * @FilePath: \lix-admin-server\app\service\login.js
 */

const Service = require("egg").Service;

class LoginService extends Service {
  /**
   * @author: lihaogn
   * @Date: 2021-12-22 20:47:36
   * @description: 登录
   * @param {*} username - 用户名
   * @param {*} password - 密码
   * @return {*}
   */
  async loginCheck({ username, password }) {
    const { app } = this;

    // const options = {
    //   where: { status: 1, name: username, password: password },
    //   columns: ["id", "name", "status"],
    // };
    // try {
    //   const res = await app.mysql.select("users", options);
    //   console.log("登录", res); // 登录 [ RowDataPacket { name: 'lihaogn' } ]
    //   if (res.length > 0) {
    //     return res[0];
    //   }
    //   return false;
    // } catch (err) {
    //   console.log(err);
    //   return false;
    // }

    try {
      const user = await app.model.User.findOne({
        where: {
          name: username,
          password,
        },
      });
      if (user) {
        if (user.status !== 1) {
          return {
            id: undefined,
            message: "您的账户已停用或注销！",
          };
        }
        // 查到用户
        return {
          id: user.id,
          // name: user.name,
          // status: user.status,
        };
      }
      // 没有查到用户
      return false;
    } catch (error) {
      console.log("登录err：", error);
      return false;
    }
  }
}

module.exports = LoginService;
